﻿using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.Publishing;
using System.Collections.Specialized;
using Inline.SharePoint.ExtendedVariations.Helpers;

namespace Inline.SharePoint.ExtendedVariations.Events
{

    /// <summary>
    /// List Item Events
    /// </summary>
    public class ItemAddedEventHandler : SPItemEventReceiver
    {

        /// <summary>
        /// An item was added.
        /// </summary>
        public override void ItemAdded(SPItemEventProperties properties)
        {

            if (PublishingWeb.IsPublishingWeb(properties.Web))
            {

                PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(properties.Web);
                StringCollection variationUrls = publishingWeb.VariationPublishingWebUrls;

                try
                {

                    ItemHandler itemHandler = new ItemHandler();
                    itemHandler.AddItem(variationUrls, properties);

                }
                catch (Exception ex)
                {

                    Logger logger = new Logger();
                    logger.WriteEntry(properties.Web.Url,
                        "Inline.SharePoint.ExtendedVariations.Events::ItemAddedEventHandler::ItemAdded",
                        ex.Message + ex.StackTrace,
                        Logger.LogLevel.Error);

                }

            }

        }

    }

}